package com.example.demo.hutool.redis.cluster;

import com.example.demo.hutool.redis.ssl.SocketFactory;
import redis.clients.jedis.*;


import javax.net.ssl.SSLSocketFactory;
import java.util.*;

public class JedisClusterTest {
    public static void main(String[] args) throws Exception {


        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        // 最大连接数
        jedisPoolConfig.setMaxTotal(10);
        // 最大连接阻塞等待时间
        jedisPoolConfig.setMaxWaitMillis(-1);
        // 最大空闲连接数
        jedisPoolConfig.setMaxIdle(1);
        // 最小空闲连接数
        jedisPoolConfig.setMinIdle(1);
        jedisPoolConfig.setTestOnBorrow(true);
        jedisPoolConfig.setTestOnReturn(true);
        HashSet<HostAndPort> hostAndPorts = new HashSet<>();
        hostAndPorts.add(new HostAndPort("172.20.54.122",7291));
        hostAndPorts.add(new HostAndPort("172.20.54.122",7292));
        hostAndPorts.add(new HostAndPort("172.20.54.122",7293));
        hostAndPorts.add(new HostAndPort("172.20.54.122",7294));
        hostAndPorts.add(new HostAndPort("172.20.54.122",7295));
        hostAndPorts.add(new HostAndPort("172.20.54.122",7296));
        //    public JedisCluster(Set<HostAndPort> jedisClusterNode, int connectionTimeout, int soTimeout, int maxAttempts, String password, String clientName, GenericObjectPoolConfig<Jedis> poolConfig, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap hostAndPortMap) {
        SSLSocketFactory socketFactory = SocketFactory.getSocketFactory("ca.crt", "redis.crt", "redis.key", "123456");
        JedisCluster jedisCluster = new JedisCluster(hostAndPorts,1000,100,100,"123456","sxm",jedisPoolConfig,true,socketFactory,null,null,null);
//          new Thread(()->{
//              System.out.println(jedisCluster.get("1"));
//              try {
//                  Thread.sleep(70000000);
//              } catch (InterruptedException e) {
//                  throw new RuntimeException(e);
//              }
//
//          }).start();
//        new Thread(()->{
//            System.out.println(jedisCluster.get("1"));
//            try {
//                Thread.sleep(70000000);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//
//        }).start();


        Set<String> result = new HashSet<>();
        // 获取Redis集群内所有节点
        Map<String, JedisPool> clusterNodes = jedisCluster.getClusterNodes();

        for (Map.Entry<String, JedisPool> stringJedisPoolEntry : clusterNodes.entrySet()) {
            String key = stringJedisPoolEntry.getKey();
            JedisPool jedisPool = stringJedisPoolEntry.getValue();
            Jedis resource = jedisPool.getResource();
            Set<String> keys = resource.keys("*");
            System.out.println("node:--"+key+"---dbsize:"+keys.size());


        }



        //  jedisCluster.set("1","1");


    }
}
